<?php


namespace app\api\controller;


use app\common\model\BetsLog;
use app\common\model\BetsLogSubscribe;
use app\common\model\GamblerKingInvestmentUser;
use app\common\model\LotteryResult;
use app\common\model\LotteryRound;
use app\common\model\Member;
use app\common\model\PlanBetsLog;
use app\common\model\PlanUserProgramme;
use app\common\model\ThirdPlanProgramme;
use app\common\model\ThirdPlanUserProgramme;
use think\Controller;
use think\Db;
use think\Queue;

class Planapi extends Controller
{
    //自动挂机
    public function plan_auto()
    {
        $list=PlanUserProgramme::all(['state'=>1]);
//        dd($list);
        //同步下注
        $jobHandlerClassName  = 'app\api\job\Plan';

        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
        $jobQueueName  	  = "PlanJobQueue";

        foreach ($list as $k=>$v){
            if($v['is_stop_time']==1 ){
                if( $v['end_time']<TIME_NOW || $v['start_time']>TIME_NOW)
                continue;
            }
            //当前期数
            $round=LotteryRound::where(['lottery_id'=>$v->lottery_id,'state'=>2])->value('round');

            if($round) {
                if ($this->is_simulation == 1) {
                    $info = BetsLogSubscribe::where(['round' => $round-1, 'user_id' => $v->user_id, 'lottery_id' => $v->lottery_id,'programme_id'=>$v['id']])->find();
                    if ($info) {
                         if($info['state']==1){
                             continue;
                         }
                    }
                }else{
                    $info = BetsLog::where(['round' => $round-1, 'user_id' => $v->user_id, 'lottery_id' => $v->lottery_id,'programme_id'=>$v['id']])->find();
                    if ($info) {
                        if($info['state']==1){
                            continue;
                        }

                    }
                }
                Queue::push($jobHandlerClassName,$v['id'], $jobQueueName);
            }


        }

    }


    //赌王产品对冲

    public function  king_plan_hedging(){
        $jobHandlerClassName  = 'app\api\job\Third';

        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
        $jobQueueName  	  = "ThirdJobQueue";
        $list=GamblerKingInvestmentUser::where(['state'=>3,'pattern'=>2,'king_win_account'=>['gt',0]])->select();
        foreach ($list as $k=>$v){
            $plan_info=ThirdPlanUserProgramme::get(['user_investment_id'=>$v['id']]);
            $plan_ids=explode(',',$plan_info['plan_ids']);
            foreach ($plan_ids as $ke=>$va){
                $plan=ThirdPlanProgramme::get(['plan_id'=>$va,'user_investment_id'=>$v['id']]);
                if($plan['state']==1){
                    if(!cache('hedging_king'.$v['id'].'_'.$va)){
                        Queue::push($jobHandlerClassName,['plan_id'=>$va,'user_investment_id'=>$v['id'],'pro_id'=>$plan_info['id']], $jobQueueName);
                        cache('hedging_king'.$v['id'].'_'.$va,$va,240);
                    }
                }


            }
        }
    }


    /**
     * 测试对冲挂机
     *
     */
    public function test_plan(){
        Db::startTrans();
        $data=['plan_id'=>1,'user_investment_id'=>14,'pro_id'=>3];
        $info=\app\common\model\ThirdPlan::get($data['plan_id']);
        try{
            $res=$info->plan_auto($data);
            d($res);
            // 提交事务
            Db::commit();
            echo 11;
        } catch (\Exception $e) {
            d($e->getMessage());
            // 回滚事务
            Db::rollback();
            echo 22;

        }
    }









}